package dhm.com.dhmshop.framework.module.home.fragment;


import android.annotation.SuppressLint;
import android.content.Intent;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.facebook.drawee.view.SimpleDraweeView;
import com.jcodecraeer.xrecyclerview.XRecyclerView;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

import dhm.com.dhmshop.R;
import dhm.com.dhmshop.adapter.mine.ShopAdapter;
import dhm.com.dhmshop.base.Presenter.PressenterImpl;
import dhm.com.dhmshop.base.netWork.Constant;
import dhm.com.dhmshop.base.netWork.LoginContract;
import dhm.com.dhmshop.entity.GetMyShopinfo;
import dhm.com.dhmshop.entity.GetOneCategory;
import dhm.com.dhmshop.framework.base.BaseFragment;
import dhm.com.dhmshop.framework.base.MyBaseViewHolder;
import dhm.com.dhmshop.framework.module.commodity.activity.CommodityDetailsActivity;
import dhm.com.dhmshop.framework.module.home.activity.ListHotGoodsActivity;
import dhm.com.dhmshop.framework.utils.GlideUtil;
import dhm.com.dhmshop.framework.utils.StringUtil;
import dhm.com.dhmshop.framework.module.home.entity.CategoryTypeGoodsEntity;
import dhm.com.dhmshop.framework.module.home.model.HomeCategoryTypeModel;
import dhm.com.dhmshop.framework.module.home.view.HomeCategoryTypeView;
import dhm.com.dhmshop.utils.SpUtils;

public class HomeTabTypeFragment extends BaseFragment<HomeCategoryTypeModel> implements LoginContract.IView {

    private int pid;
    private int position;
    private TopTypeAdapter topTypeAdapter;
    private GoodsListAdapter goodsListAdapter;
    private List<CategoryTypeGoodsEntity.CatelistBean> catelist;
    private String uid;
    private PressenterImpl pressenter;
    private ShopAdapter shopAdapter;
    private XRecyclerView goodsListRecycle;
    private LinearLayout image;
    private GetOneCategory.ChildCategory result;
    private List<GetMyShopinfo.DataBean> myShopinfoData;
    private int isclick;

    public HomeTabTypeFragment() {
    }

    @SuppressLint("ValidFragment")
    public HomeTabTypeFragment(int id, int position) {
        pid = id;
        this.position = position;
    }


    @Override
    protected void initView(View inflate) {
        uid = SpUtils.getString(getActivity(), "uid");
        RecyclerView topTypeRecycle = findViewById(R.id.topTypeRecycle);
        goodsListRecycle = findViewById(R.id.goodsListRecycle);
        image = findViewById(R.id.image);
        offset = 1;
        isclick = 0;
        GridLayoutManager manager=new GridLayoutManager(getActivity(),2);
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        goodsListRecycle.setLayoutManager(manager);
        goodsListRecycle.setLoadingMoreEnabled(true);
        goodsListRecycle.setPullRefreshEnabled(true);
        topTypeAdapter = new TopTypeAdapter();
        topTypeRecycle.setAdapter(topTypeAdapter);
        pressenter = new PressenterImpl();
        pressenter.attachView(this);
    }

    @Override
    protected void initData() {
        Map<String,String> map=new HashMap<>();
        map.put("token", Constant.TOKEN);
        map.put("pid",pid+"");
        pressenter.sendMessage(getActivity(),Constant.GetChildCategory,map,GetOneCategory.ChildCategory.class);
    }

    private int offset = 1;

    @Override
    protected void initListener() {
        //设置头部分类条目点击监听
        topTypeAdapter.setOnItemClickListener((adapter, view, position) -> {
            isclick=1;
            offset=1;
            Map<String, String> map = new HashMap<>();
            map.put("token", Constant.TOKEN);
            map.put("page", offset + "");
            map.put("cate_id", result.getData().get(0).getChild().get(position).getCatId() + "");
            map.put("uid", uid);
            pressenter.sendMessage(getActivity(), Constant.CateSearch, map, GetMyShopinfo.class);
            goodsListRecycle.setLoadingListener(new XRecyclerView.LoadingListener() {
                @Override
                public void onRefresh() {
                    offset=1;
                    Map<String, String> map = new HashMap<>();
                    map.put("token", Constant.TOKEN);
                    map.put("page", offset + "");
                    map.put("cate_id", result.getData().get(0).getChild().get(position).getCatId() + "");
                    map.put("uid", uid);
                    pressenter.sendMessage(getActivity(), Constant.CateSearch, map, GetMyShopinfo.class);
                }

                @Override
                public void onLoadMore() {
                    Map<String, String> map = new HashMap<>();
                    map.put("token", Constant.TOKEN);
                    map.put("page", offset + "");
                    map.put("cate_id", result.getData().get(0).getChild().get(position).getCatId() + "");
                    map.put("uid", uid);
                    pressenter.sendMessage(getActivity(), Constant.CateSearch, map, GetMyShopinfo.class);
                }
            });


        });
    }

    @Override
    protected HomeCategoryTypeModel initModel() {
        return new HomeCategoryTypeModel();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_home_child;
    }

/*
    @Override
    public void onCategoryGoodsListSuccess(CategoryTypeGoodsEntity categoryTypeGoodsEntity) {
        catelist = categoryTypeGoodsEntity.getCatelist();
        if (categoryTypeGoodsEntity.getCatelist() != null && categoryTypeGoodsEntity.getCatelist().size() > 0) {

            goodsListAdapter = new GoodsListAdapter();
            goodsListRecycle.setAdapter(goodsListAdapter);
            topTypeAdapter.addData(categoryTypeGoodsEntity.getCatelist());
        }
        if (categoryTypeGoodsEntity.getGoods() != null && categoryTypeGoodsEntity.getGoods().size() > 0) {
            goodsListAdapter.addData(categoryTypeGoodsEntity.getGoods());
        }
    }
*/

    @Override
    public void requesta(Object data) {
        if (data instanceof GetMyShopinfo) {
            GetMyShopinfo getMyShopinfo = (GetMyShopinfo) data;
            if (getMyShopinfo.getCode() == 1) {
                goodsListRecycle.setVisibility(View.VISIBLE);
                image.setVisibility(View.GONE);
                if (offset==1){
                    shopAdapter = new ShopAdapter(getActivity());
                    goodsListRecycle.setAdapter(shopAdapter);
                    myShopinfoData = getMyShopinfo.getData();
                    shopAdapter.setaddClick(new ShopAdapter.OnIntentClick() {
                        @Override
                        public void item(int posttion) {
                            Intent intent = new Intent(getActivity(), CommodityDetailsActivity.class);
                            intent.putExtra("gid", getMyShopinfo.getData().get(posttion).getGoods_id());
                            intent.putExtra("shop_id", getMyShopinfo.getData().get(posttion).getShop_id());
                            startActivity(intent);
                        }
                    });
                }else {
                    myShopinfoData.addAll(getMyShopinfo.getData());
                }
                offset++;
                shopAdapter.setShopList(myShopinfoData);
            } else {
                if (offset==1){
                    goodsListRecycle.setVisibility(View.GONE);
                    image.setVisibility(View.VISIBLE);
                    if (isclick==1){
                        Toast.makeText(context, "暂无数据", Toast.LENGTH_SHORT).show();
                    }
                }else {
                    Toast.makeText(context, "暂无更多了", Toast.LENGTH_SHORT).show();
                }
            }
            goodsListRecycle.refreshComplete();
            goodsListRecycle.loadMoreComplete();
        } else if (data instanceof GetOneCategory.ChildCategory) {
            result = (GetOneCategory.ChildCategory) data;
            if (result.getCode() == 1) {
                topTypeAdapter.addData(result.getData().get(0).getChild());
                Map<String, String> map = new HashMap<>();
                map.put("token", Constant.TOKEN);
                map.put("page", offset + "");
                map.put("cate_id", pid + "");
                map.put("uid", uid);
                pressenter.sendMessage(getActivity(), Constant.CateSearch, map, GetMyShopinfo.class);

                goodsListRecycle.setLoadingListener(new XRecyclerView.LoadingListener() {
                    @Override
                    public void onRefresh() {
                        offset=1;
                        Map<String, String> map = new HashMap<>();
                        map.put("token", Constant.TOKEN);
                        map.put("page", offset + "");
                        map.put("cate_id", pid + "");
                        map.put("uid", uid);
                        pressenter.sendMessage(getActivity(), Constant.CateSearch, map, GetMyShopinfo.class);
                    }

                    @Override
                    public void onLoadMore() {
                        Map<String, String> map = new HashMap<>();
                        map.put("token", Constant.TOKEN);
                        map.put("page", offset + "");
                        map.put("cate_id", pid + "");
                        map.put("uid", uid);
                        pressenter.sendMessage(getActivity(), Constant.CateSearch, map, GetMyShopinfo.class);
                    }
                });


            }
        }
    }

    @Override
    public void fail(String error) {

    }

    /**
     * 顶部分类列表 适配器
     */
    class TopTypeAdapter extends BaseQuickAdapter<GetOneCategory.ChildCategory.DataBean.ChildBean, MyBaseViewHolder> {

        public TopTypeAdapter() {
            super(R.layout.getchildgorygoodsoneitem);
        }

        @Override
        protected void convert(MyBaseViewHolder helper, GetOneCategory.ChildCategory.DataBean.ChildBean item) {
            helper.setText(R.id.name, item.getSimpleName());
            ImageView thumb = helper.getView(R.id.thumb);
            Glide.with(context).load(Constant.PATH + "/" + item.getCatImg()).into(thumb);
        }
    }

    /**
     * 商品列表 适配器
     */

    class GoodsListAdapter extends BaseQuickAdapter<CategoryTypeGoodsEntity.GoodsBean, MyBaseViewHolder> {

        public GoodsListAdapter() {
            super(R.layout.item_shop);
        }

        @Override
        protected void convert(MyBaseViewHolder helper, CategoryTypeGoodsEntity.GoodsBean item) {
            helper.setText(R.id.name, StringUtil.preventNull(item.getGoods_name()))
                    .setText(R.id.money, "￥"+StringUtil.preventNull(item.getPrice()))
                    .setText(R.id.pay, StringUtil.buildString(item.getSale_num(), "人付款"));
            SimpleDraweeView view = helper.getView(R.id.image);
            view.setImageURI(Constant.PATH + item.getGoods_images());
            helper.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent intent = new Intent(context, CommodityDetailsActivity.class);
                    intent.putExtra("gid", item.getGoods_id());
                    intent.putExtra("shop_id", item.getShop_id());
                    context.startActivity(intent);
                }
            });

        }
    }
}
